package com.jizzotool.client;

import com.jizzotool.shared.FieldVerifier;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.event.dom.client.KeyUpHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class Jizzotool implements EntryPoint {
	/**
	 * The message displayed to the user when the server cannot be reached or
	 * returns an error.
	 */
	private static final String SERVER_ERROR = "An error occurred while "
			+ "attempting to contact the server. Please check your network "
			+ "connection and try again.";

	/**
	 * Create a remote service proxy to talk to the server-side Greeting service.
	 */
	private final LoginServiceAsync loginService = GWT
			.create(LoginService.class);

	/**
	 * This is the entry point method.
	 */
	public void onModuleLoad() {
		Button loginButton = new Button("Login");
		final TextBox textLogin = new TextBox();
		final PasswordTextBox textPassword = new PasswordTextBox();
		Label labelLogin = new Label("Login : ");
		Label labelPassword = new Label("Password : ");
		HorizontalPanel loginPanel = new HorizontalPanel();
		HorizontalPanel passwordPanel = new HorizontalPanel();
		VerticalPanel mainPanel = new VerticalPanel();
		loginPanel.add(labelLogin);
		loginPanel.add(textLogin);
		passwordPanel.add(labelPassword);
		passwordPanel.add(textPassword);
		mainPanel.add(loginPanel);
		mainPanel.add(passwordPanel);
		mainPanel.add(loginButton);
		RootPanel.get().add(mainPanel);
		
		loginService.loginFromSession(new AsyncCallback<User>() {
			public void onFailure(Throwable caught) {
				Window.alert("Erreur");
			}

			public void onSuccess(User user) {
				if (user != null) {
					Window.alert("Deja loggé !");
				} else {
					Window.alert("Vous devez vous logger");
				}
			}
		});
		
		loginButton.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				loginService.login(textLogin.getText(), textPassword.getText(), new AsyncCallback<User>() {
					public void onFailure(Throwable caught) {
						Window.alert("Erreur");
					}

					public void onSuccess(User user) {
						Window.alert(user.getLogin() + " - " + user.getPassword());
					}
				});
			}
		});
	}
}
